/* ************************************************************************
> File Name:     reverseList.c
> Author:        程序员lyl
> 微信公众号:    雨特宙
> Created Time:  2024年04月09日 星期二 21时39分06秒
> Description:   
 ************************************************************************/
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode{
    int val;
    struct ListNode* next;
    
} ListNode_t;

void addList(ListNode_t* head, int val){
   // ListNode_t* dummyHead = (ListNode_t *)malloc(sizeof(ListNode_t));
    //dummyHead->next = NULL;
    ListNode_t* cur = head;
    while(cur->next){
        cur = cur->next;
    }
    ListNode_t* tail = (ListNode_t *)malloc(sizeof(ListNode_t));
    tail->val = val;
    tail->next = NULL;
    cur->next = tail;
    
    
}

ListNode_t* revertList(ListNode_t* head){
    ListNode_t* pre = NULL;
    ListNode_t* cur = head;
    ListNode_t* tmp;
    while(cur){
        tmp = cur->next;
        cur->next = pre;
        pre = cur;
        cur = tmp;
    }
    return pre;
    
    
}

void printList(ListNode_t* head){
    while(head){
        printf("%d ",head->val);
        head = head->next;
    }
    printf("\n");
}


int main(){
    int val;
    int n;
    int m;
    while(scanf("%d",&n) != EOF){
        if(n == 0){
            printf("list is empty\n");
        }
        else{
            ListNode_t* dummyHead = (ListNode_t *)malloc(sizeof(ListNode_t));
            dummyHead->next = NULL;
            ListNode_t* cur = dummyHead;
            for(int i = 0; i < n; i++){
                scanf("%d",&val);
                addList(cur, val);
                cur = cur->next; //好处就在于及时的将cur指向链表为尾结点
               /*
                scanf("%d", &m);
                ListNode_t* newNode = (ListNode_t*)malloc(sizeof(ListNode_t)); // 开始构造节点
                newNode->val = m;
                newNode->next = NULL;
                cur->next = newNode;
                cur = cur->next;
*/
            }
            printList(dummyHead->next);
            
            //ListNode_t* Rhead = revertList(dummyHead->next);
            printList(revertList(dummyHead->next));
        }
        
        
    }
    
}
